package greatwall.myreader;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.iximo.domain.Category;
import com.iximo.openAPI.impl.IximoOpenAPIGetImpl;
import com.iximo.util.OpenAPIClient;
import greatwall.myreader.OpenAPIClientFactory;
import greatwall.myreader.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ShowCategoryNameList extends Activity
{
	private TextView TVResult;
	private Button BTNSearch;
	private ProgressDialog progressDialog;
	private ListView list;
	private IximoOpenAPIGetImpl Get = new IximoOpenAPIGetImpl();
	private ArrayList<HashMap<String,Object > > listItem = null;
	private SimpleAdapter listItemAdapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categoryname_main);
    	progressDialog = ProgressDialog.show(ShowCategoryNameList.this, 
				"Loading...", "Please wait...", true, false);  
        GetControls();
        Display();
    }
   
    private void GetControls()
    {
    	TVResult = (TextView)findViewById(R.id.resultContent);    	
    	list = (ListView) findViewById(R.id.ListView01);
    }
    private OpenAPIClient XiMoClient = OpenAPIClientFactory.GetInstance();    
    
    
    
	public void ShowCategoryList() {
		// TODO Auto-generated method stub
		//开启progressDialog，显示等待框
	
	    final String string = "书籍分类" ;
		//启动线程获取图书信息
		new Thread(){
			@Override
			public void run()
			{						
				try
				{
					Message msg = new Message();
					Bundle b = new Bundle();
					b.putString("bookinfo",string);
					msg.setData(b);
					handler.sendMessage(msg);
					
				}
				catch(Exception e)
				{
					//没有获取到，发送异常消息
					Message msg = new Message();
					Bundle b = new Bundle();
					b.putString("bookinfo", "获取失败");
					msg.setData(b);
					handler.sendMessage(msg);
				}
			}
		}.start();
	}

	private void Display()
	{
		listItem = new ArrayList<HashMap<String, Object>>();
		//生成适配器的Item和动态数组对应的元素   
        listItemAdapter = new SimpleAdapter(ShowCategoryNameList.this,listItem,//数据源    
            R.layout.categoryname_user,//ListItem的XML实现   
            //动态数组与ImageItem对应的子项           
            new String[] {"ItemImage","CategoryName", },    
            //ImageItem的XML文件中的一张图片和三个TextView ID   
            new int[] {R.id.ItemImage,R.id.CategoryName}   
        );  
        list.setAdapter(listItemAdapter);
        
        ShowCategoryList();
	}
    
	//处理线程发送过来的消息
    private Handler handler = new Handler(){
    	@Override
    	public void handleMessage(Message msg)
    	{
			try 
			{
				progressDialog.dismiss(); 
				String getstring = XiMoClient.get_book_category("");
				InputStream inputStream = new ByteArrayInputStream(getstring.getBytes());
				List<Category> categoryList = Get.getAllCategoryData(inputStream);
		         //生成动态数组，加入数据   
		        
				for(int i=0;i<categoryList.size();i++)   
		        {   
		            HashMap<String, Object> map = new HashMap<String, Object>();     
		            map.put("ItemImage", R.drawable.book_bg_shadow);//图像资源的ID
//		            map.put("ItemImage",bookListByNumData.get(i).getLimageURL());
		            map.put("CategoryName",categoryList.get(i).getCategoryName());   
		            map.put("CategoryID", categoryList.get(i).getCategoryID());
		            listItem.add(map);   
		        }   
		        
				listItemAdapter.notifyDataSetChanged();
			    list.setOnItemClickListener(ListItemListener);
			} catch (Exception e) 
			{
				// TODO Auto-generated catch block
				progressDialog.dismiss();
				TVResult.setText("aaaa");
			}
			
    	}
    };
    private OnItemClickListener ListItemListener = new OnItemClickListener()
    {

		@Override
		public void onItemClick(AdapterView<?> arg0,View arg1, int arg2,long arg3) 
		{
			// TODO Auto-generated method stub
			try 
			{   				
				//Log.i("1",listItem.get((int)arg3).get("CategoryID").toString());
				Intent intent = new Intent();
				Log.w("error","1");
				intent.putExtra("getString",listItem.get((int)arg3).get("CategoryID").toString());
				Log.w("error",listItem.get((int)arg3).get("CategoryID").toString());
			    intent.setClass(ShowCategoryNameList.this,ShowBookListByCategory.class);
			    Log.w("error","3");
			    startActivity(intent);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//TVResult.setText("cccc");
			}
	}
    	
 };
}
